/**
 * 用户数据存储
 */
import encryptionRAS from 'utils/jsencrypt.ts';

//用户信息加密保存到浏览器存储中
export const useUsersStore = defineStore(
    'useUsers',
    () => {
        const useDataRct = reactive([]);
        const getUsersCpu = encryptionRAS.decrypt(useDataRct);
        //@ts-ignore
        const setUsers = (value: any) => useDataRct.push(encryptionRAS.encrypt(value));
        return {
            useDataRct,
            getUsersCpu,
            setUsers
        };
    },
    {
        persist: {
            //持久化存储
            enabled: true,
            strategies: [
                { key: 'userData', storage: localStorage, paths: ['useDataRct'] }
                // { storage: sessionStorage, paths: ['useDataRct']},
            ]
        }
    }
);

//用户使用中未返回的接口保存
export const useQueueStore = defineStore('useQueue', () => {
    const useQueueRef = ref({});
    const setUsers = (value: any) => (useQueueRef.value = value);
    const cancelAllRequest = () => {
        Object.values(useQueueRef.value).forEach((item: any) => item());
        useQueueRef.value = {};
    };
    const cancelRequestByUrl = (url: string) => (useQueueRef.value as any)[url]();
    return {
        useQueueRef,
        setUsers,
        cancelAllRequest,
        cancelRequestByUrl
    };
});
